/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package lbim.protocol;

import javax.microedition.location.Location;
import javax.microedition.location.LocationException;
import javax.microedition.location.LocationProvider;
import javax.microedition.location.QualifiedCoordinates;
import lbim.client.model.Account;
import lbim.client.model.LocXy;
import lbim.client.model.LocationManager;

/**
 *
 * @author Jestan
 */
public final class Controller {
    private Account account;
    public  static final int DEFAULT_MIN_ZOOM = 6;
    public  static final int DEFAULT_MAX_ZOOM = 16;
    public static LocationManager locationManager;
    public static String cellId;
    private String mapResolution; // 240x160

    public Controller(Account account) throws LocationException {
        init();
        this.account = account;
    }

    public Controller() throws LocationException {
        init();
    }

    public void setAccount(Account account) {
        this.account = account;
    }

    private void init() throws LocationException {
        if (System.getProperty("microedition.location.version") != null) {
            System.out.println("[Controller] Location API impl exists");
            locationManager = new LocationManager();
  
        } else {
            cellId = System.getProperty("com.nokia.mid.cellid");
            if (cellId == null) {
                System.out.println("[Controller] failed to get cell id");
            }
        }
    }

    public void connect() {
        LbImProtocol protocol = new LbImProtocol(account);
        account.setProtocol(protocol);
        protocol.sendLoginMessage();
    }

    public void disconnect() {
        LbImProtocol protocol = account.getProtocol();
        protocol.sendLogoutMessage();
    }

    public void sendLocationUpdate() {
        sendLocationUpdate(DEFAULT_MIN_ZOOM);
    }

    public void sendLocationUpdate(int zoom) {
        locationManager.setMapUpdateListener(account.getMapUpdateListener());
        LocXy currentXy = locationManager.getCurrentLocation();
        account.getProtocol().sendLocationUpdate(currentXy, mapResolution, zoom);
    }

    public void sendChatMessage(String message, String receiver) {
        account.getProtocol().sendChatMessage(message, receiver);
    }

    public void sendPresenceReqMessage() {
        account.getProtocol().sendPresenceReqMessage();
    }

    public String getMapResolution() {
        return mapResolution;
    }

    public void setMapResolution(String mapResolution) {
        this.mapResolution = mapResolution;
    }

}
